<template>
<div class="long_conter">
  <div class="login_box">
    <!--头像区域-->
    <div class="avatar_box">
      <img src="../assets/logo.png" alt="">
    </div>
    <!--表单区域-->
    <el-form ref="loginFormRef" label-width="0px" :model="form" class="login_form" :rules="loginFormRules">
      <!--用户名 图标使用阿里prefix-icon="iconfont icon-user"-->
      <el-form-item prop="name">
        <el-input prefix-icon="iconfont icon-user" v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <!--密码-->
      <el-form-item prop="password">
        <el-input type="password" prefix-icon="iconfont icon-3702mima" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <!--按钮-->
      <el-form-item class="buts">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetLoginForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      //登录表单数据绑定对象
      form: {
        username: 'admin',
        password: '123456'
      },
      //表单验证规则
      loginFormRules: {
        username: [{
            required: true,
            message: '请输用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [{
            required: true,
            message: '输入登录密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    //点击重置
    resetLoginForm() {
      // console.log(this)表单重置
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      //登录验证获取表单引用对象 this.$refs.loginFormRef
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return;
        this.$http.post('login', this.form).then(res => {
          if (res.data.meta.status !== 200) return this.$message.error('登录失败')
          //使用弹框组件success成功组件
          this.$message.success('登录成功')
          //1.将登录成功之后的token保存到客户端的sessionStorage中
          //1.1项目中登录之外其他的API接口 必须在登录之后才能访问
          //1.2 token只应在等钱网站打开期间生效 ，所以token保存在sessionStorage
          window.sessionStorage.setItem("token", res.data.data.token);
          //2.通过编程式导航跳转到后台主页，路由地址式/home
          this.$router.push("/home")
        })
      })
    }
  },
}
</script>

<style lang="less" scoped>
.long_conter {
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 80%;
  padding: 10px;
  // 设置阴影
  box-shadow: 0 0 10px #ddd;
  margin: -15% auto;
  background-color: #fff;

  img {
    widows: 100%;
    height: 100%;
    border-radius: 80%;
    background-color: #eee;
  }
}

.buts {
  display: flex;
  justify-content: center;
}

.login_form {
  margin-top: 20%;
  padding: 0 20px;
}
</style>
